from data_capture.etf_data_capture.get_codes import *
from data_capture.etf_data_capture.get_price import Get_price
import sys 
sys.path.append(r'code')
from data_capture.data_contrate import Data_consentrate
import shutil


def get_etf_related():
    get_etf_codes()

    etf = pd.read_csv(r'data/etf_data/code_date/etf_codes.csv')
    etf = etf['代码'].values

    shutil.rmtree('data\etf_data\daily_price')
    G = Get_price()
    G.get_price(etf)


    lst = ['open', 'high', 'low', 'close', 'volume', 'vol', 'lagret']
    for i in lst:
        D = Data_consentrate(root=r'data/etf_data/daily_price', thred=300)
        D.consentrate(key=i)

def data_fliter():
    root = r'data\etf_data\consentrate_daily_price'
    lst = ['open', 'high', 'low', 'close', 'volume', 'vol', 'lagret']
    vol = pd.read_pickle(os.path.join(root, 'vol.pkl.gzip'))
    sortv = vol.mean().sort_values()
    sortv_id = sortv[sortv>0.01].index
    for i in lst:
        file = os.path.join(root,f'{i}.pkl.gzip')
        data_i = pd.read_pickle(file)
        data_i = data_i[sortv_id]
        data_i.to_pickle(file)




import pandas as pd
import os
import numpy as np
import matplotlib.pyplot as plt 
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

def get_base_etf():
    etf = pd.read_csv(r'data/etf_data/code_date/etf_codes.csv')
    etf.index = etf['代码']

    root = r'data\etf_data\consentrate_daily_price'
    close = pd.read_pickle(os.path.join(root,'close.pkl.gzip'))
    high = pd.read_pickle(os.path.join(root,'close.pkl.gzip'))
    low = pd.read_pickle(os.path.join(root,'close.pkl.gzip'))
    nan_num = len(close)-(np.isnan(close)).astype(int).sum()
    lagret = np.log(close/close.shift(1))
    lagret[abs(lagret)>0.1] = 0
    close = (lagret+1).cumprod(axis=0)

    poc = (close - close.rolling(1000, min_periods=1).min())/(close.rolling(1000, min_periods=1).max()-close.rolling(1000, min_periods=1).min())
    tp = poc.iloc[-1].sort_values()

    poc2 = (close - close.rolling(2000, min_periods=1).min())/(close.rolling(2000, min_periods=1).max()-close.rolling(2000, min_periods=1).min())
    tp2 = poc2.iloc[-1].sort_values()

    poc3 = (close - close.rolling(500, min_periods=1).min())/(close.rolling(500, min_periods=1).max()-close.rolling(500, min_periods=1).min())
    tp3 = poc3.iloc[-1].sort_values()

    tp_name = etf.loc[tp2.index]
    poc_t = pd.concat([tp3,tp,tp2,tp_name[['名称','最新价','涨跌额']], nan_num], axis=1)
    poc_t.columns = ['500', '1000', '2000', '名称','最新价','涨跌额','nan_num'] 
    poc_t = poc_t.sort_values(by='500')

    ebs = pd.read_pickle(r'data\stock_data\daily\other\ebs_lg.pkl.gzip')
    ebs_poc_1000 = (ebs - ebs.rolling(1000, min_periods=1).min())/(ebs.rolling(1000, min_periods=1).max()-ebs.rolling(1000, min_periods=1).min())
    # ebs_poc_2000 = (ebs - ebs.rolling(2000, min_periods=1).min())/(ebs.rolling(2000, min_periods=1).max()-ebs.rolling(2000, min_periods=1).min())
    # ebs_poc = pd.concat([ebs_poc_1000[['沪深300指数','股债利差']],ebs_poc_2000[['沪深300指数','股债利差']]], axis=1)
    # ebs_poc.columns = ['hs300_1000', 'sb_diff_1000', 'hs300_2000', 'sb_diff_2000']
    ebs_poc = ebs_poc_1000
    ebs_poc.plot()
    plt.savefig(r'result\etf\bound_stock_diff.png')
    print('fin')

    hold_lst = ['sh510630','sh510100','sz159869','sz159781','sz159929', 'sh515650' ,'sh516640','sz159732','sh588180','sh513330','sz159755', 'sh513050']
    hold_poc = poc_t.loc[hold_lst,:].sort_values(by='500', ascending=False)
    return poc_t[:30], ebs_poc.iloc[-30:], hold_poc

from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
def send_email(content):
    #qq邮箱smtp服务器
    host_server = 'smtp.qq.com'
    #sender_qq为发件人的qq号码
    sender_qq = '1639682209@qq.com'
    #pwd为qq邮箱的授权码
    pwd = 'XXXXX' ## xh**********bdc
    #发件人的邮箱
    sender_qq_mail = '1639682209@qq.com'
    #收件人邮箱
    receiver = '1639682209@qq.com'

    #邮件的正文内容
    mail_content = content
    #邮件标题
    mail_title = 'etf信息'
    msg = MIMEMultipart('related')

    msgText = MIMEText(str(mail_content), "plain", 'utf-8')
    msg.attach(msgText)

    msg["Subject"] = Header(mail_title, 'utf-8')
    msg["From"] = sender_qq_mail
    msg["To"] = receiver
    smtp = smtplib.SMTP()
    smtp.connect(host_server)
    smtp.login(sender_qq, pwd)
    smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
    smtp.quit()

def send():
    sender = '1639682209@qq.com'
    receivers = '1639682209@qq.com'
    message =  MIMEMultipart('related')
    subject = 'etf'
    message['Subject'] = subject
    message['From'] = sender
    message['To'] = receivers
    content = MIMEText('<html><body><img src="cid:imageid" alt="imageid"></body></html>','html','utf-8')
    message.attach(content)

    file=open(r'result\etf\bound_stock_diff.png', "rb")
    img_data = file.read()
    file.close()


    img = MIMEImage(img_data)
    img.add_header('Content-ID', 'imageid')
    message.attach(img)

    try:
        server=smtplib.SMTP_SSL("smtp.qq.com",465)
        server.login(sender,'XXXXX')
        server.sendmail(sender,receivers,message.as_string())
        server.quit()
        print ("邮件发送成功")
    except smtplib.SMTPException as e:
        print(e)
# send()

# # get_etf_related()
# data_fliter()
# poc_etf, ebs_poc, hold_poc = get_base_etf()
# send_email((poc_etf, '###########', hold_poc, '##########', ebs_poc ))
# send()



